import pygame
from pygame.locals import *
from pygame import *
import sys

class image(pygame.sprite.Sprite):
    def __init__(self, name, xpos, ypos, xsize, ysize, application):
        # Load image
        #print xpos, ypos, xsize, ysize
        image = self.loadimage(name, xpos, ypos, xsize, ysize, application)
        # Add to spritelist
        application.spritelist.append(image)
        # Render all items in spritelist
        application.spritegrouprender = pygame.sprite.OrderedUpdates(application.spritelist)
        # Draw rendered group
        application.spritegrouprender.draw(pygame.display.get_surface())
        # Show
        pygame.display.flip() 

    def loadimage(self, name, xpos, ypos, xsize, ysize, application):
        # Call sprite initializer
        pygame.sprite.Sprite.__init__(self)
        # Load image
        self.image = pygame.image.load(name).convert_alpha()
        # Rect
        self.rect = self.image.get_rect()
        xwidth = self.image.get_rect().width
        yheight = self.image.get_rect().height
        # Surfacesize
        self.tilex = pygame.display.get_surface().get_rect().width / application.divx
        self.tiley = pygame.display.get_surface().get_rect().height / application.divy
        # Backup original xsize and ysize of the loaded image
        self.xsize = xsize
        self.ysize = ysize
        bxs = xsize * self.tilex
        bys = ysize * self.tiley
        resize = 0
        # Resize
        if xsize <> 0 or ysize <> 0:
            if xsize <> 0 and resize == 0 and xwidth >= yheight:
                xsize = xsize * self.tilex
                ysize = (float(xsize)/float(xwidth))*yheight
                ysize = int(ysize)
                resize = 1
            if ysize <> 0 and resize == 0 and yheight >= xwidth:
                ysize = ysize * self.tiley
                xsize = (float(xwidth)/float(yheight))*ysize
                xsize = int(xsize)
                resize = 1
            self.image = pygame.transform.scale(self.image, (xsize, ysize))
        # Positions
        # Backup xpos and ypos
        bxp = xpos
        byp = ypos
        self.xpos = xpos
        self.ypos = ypos
        # Get actual res for x
        if xpos <> "center":
            xpos = xpos * self.tilex
        # Get actual res for y
        if ypos <> "center":
            ypos = ypos * self.tiley
        # X center
        if xpos == "center":
            xpos = pygame.display.get_surface().get_rect().centerx  - self.image.get_rect().width/2
        # Y center
        if ypos == "center":
            ypos = pygame.display.get_surface().get_rect().centery  - self.image.get_rect().height/2
        # Center automatic for tiles - we use our backup's here
        self.xoffset = 0
        self.yoffset = 0
        # X-Offset
        if bxs <> 0 and bxp <> "center":
            xwidth = self.image.get_rect().width
            self.xoffset = abs(bxs - xwidth)
            self.xoffset = int(self.xoffset/2)
        # Y-Offset
        if bys <> 0 and byp <> "center":
            yheight = self.image.get_rect().height
            self.yoffset = abs(bys - yheight)
            self.yoffset = int(self.yoffset/2)
        xpos = xpos + self.xoffset
        ypos = ypos + self.yoffset
        #print xpos, ypos
        # Set X & Y
        self.rect.width = xsize
        if xsize == 0:
            self.rect.width = self.image.get_rect().width
        self.rect.height = ysize
        if ysize == 0:
            self.rect.height = self.image.get_rect().height
        self.rect.x = xpos
        self.xpos = xpos
        self.rect.y = ypos
        self.ypos = ypos
        return self

    def change(self, name, application):
        # Remove offset
        if self.xpos <> 0 and self.xoffset <> 0:
            self.xpos = self.xpos - self.xoffset 
            self.xpos /= application.cubex
        else:
            self.xpos /= application.cubex
        if self.ypos <> 0 and self.yoffset <> 0:
            self.ypos = self.ypos - self.yoffset
            self.ypos /= application.cubey
        else:
            self.ypos /= application.cubey
        image = self.loadimage(name, self.xpos, self.ypos, self.xsize, self.ysize, application)
        application.spritegrouprender.draw(pygame.display.get_surface())
        pygame.display.flip()

    def beamto(self, x, y, application):
        # X
        if x == -1 or x == "center":
            if x == -1:
                x = 0 - self.rect.width
            if x == "center":
                x = pygame.display.get_surface().get_rect().centerx  - self.image.get_rect().width/2
        else:
            x = x * application.cubex
        # Y
        if y == -1 or y == "center":
            if y == -1:
                y = 0 - self.rect.height
            if y == "center":
                y = pygame.display.get_surface().get_rect().centery  - self.image.get_rect().height/2
        else:
            y = y * application.cubey
        # Rect
        self.rect.x = x
        self.xpos = x
        self.rect.y = y
        self.ypos = y
        application.spritegrouprender.draw(pygame.display.get_surface())
        pygame.display.flip()

    def movex(self, xt, application):
        if xt <> -1 and xt <> -2:
            xt *= application.cubex
        if xt == -1:
            xt = 0 - self.rect.width
        if xt == -2:
            xt = application.resolution[0]
        while self.rect.x <> xt:
            if self.rect.x < xt:
                self.rect.x += application.cubex
            if self.rect.x > xt:
                self.rect.x -= application.cubex
            application.spritegrouprender.draw(pygame.display.get_surface())
            pygame.display.flip()

    def movey(self, yt, application):
        if yt <> -1 and yt <> -2:
            yt *= application.cubey
        if yt == -1:
            yt = 0 - self.rect.height
        if yt == -2:
            yt = application.resolution[1]
        while self.rect.y <> yt:
            if self.rect.y < yt:
                self.rect.y += application.cubey
            if self.rect.y > yt:
                self.rect.y -= application.cubey
            application.spritegrouprender.draw(pygame.display.get_surface())
            pygame.display.flip()

    def hidebeamto(self, x, y, application):
        # THE DIFFERENT IS THAT AFTER BEAMING AN IMAGE IT WILL NOT BE RENDEREND!
        # X
        if x == -1 or x == "center":
            if x == -1:
                x = 0 - self.rect.width
            if x == "center":
                x = pygame.display.get_surface().get_rect().centerx  - self.image.get_rect().width/2
        else:
            x = x * application.cubex
        # Y
        if y == -1 or y == "center":
            if y == -1:
                y = 0 - self.rect.height
            if y == "center":
                y = pygame.display.get_surface().get_rect().centery  - self.image.get_rect().height/2
        else:
            y = y * application.cubey
        # Rect
        self.rect.x = x
        self.xpos = x
        self.rect.y = y
        self.ypos = y